package main

import (
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

func writeData(intChan chan int) {
	defer wg.Done()

	for i := 1; i <= 10; i++ {
		intChan <- i
		fmt.Println("正在写入数据：", i)
		time.Sleep(time.Second)
	}

	close(intChan)
}

func readData(intChan chan int) {
	defer wg.Done()

	for i := range intChan {
		fmt.Println("读取数据成功", i)
		time.Sleep(time.Second)
	}
}

func main() {
	// 管道与协程一起工作，出现只写不读的情况时，会出现阻塞(当Add(2)时， 如果Add(1)就没事)

	var intChan chan int = make(chan int, 10)

	wg.Add(2)
	go writeData(intChan)
	go readData(intChan)

	wg.Wait()
}
